package com.ffyc.cemp.common.dto;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * 返回前端的数据集
 * @param <T>
 */
@Data
public class HttpResult<T> {
    @ApiModelProperty(notes = "返回代码")
    private int code;
    @ApiModelProperty(notes = "返回信息")
    private String msg;
    @ApiModelProperty(notes = "返回数据")
    private T results;
    @ApiModelProperty(notes = "返回时间")
    private Date time;


    public static <T> HttpResult<T> success(T results) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setCode(ResultCode.SUCCESS.getCode());
        httpResult.setMsg(ResultCode.SUCCESS.getMsg());
        httpResult.setResults(results);
        httpResult.setTime(new Date());
        return httpResult;
    }

    public static <T> HttpResult<T> failed(T results) {
        HttpResult<T> httpResult = new HttpResult<T>();
        httpResult.setCode(ResultCode.FAILED.getCode());
        httpResult.setMsg(ResultCode.FAILED.getMsg());
        httpResult.setResults(results);
        httpResult.setTime(new Date());
        return httpResult;
    }

    public static <T> HttpResult<T> failed() {
        return failed(null);
    }
}
